<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-form
          :inline="true"
          :model="query"
          class="demo-form-inline"
          label-width="120px"
        >
          <el-form-item label="产品或配件名称">
            <el-input v-model="query.searchTxt"   style="width: 300px"/>
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <crudOperation />
    </div>
    <!--Form表单-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      append-to-body
      width="760px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="240px"
      >
        <el-form-item label="是否公共配件：" >
          <el-select  v-model="form.isPartsPublic"  style="width: 172px;" @change="isPartsPublicChange" >
            <el-option label="否" value="N"></el-option>
            <el-option label="是" value="Y"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称：" prop="productName" >
          <el-input v-model="form.productName" v-if="form.isPartsPublic ==='N' || form.isPartsPublic === undefined"></el-input>
          <el-select v-model="form.publicProductList" placeholder="请选择" v-if="form.isPartsPublic ==='Y'" multiple>
            <el-option
              v-for="item in productNameList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>

        </el-form-item>
        <el-form-item label="产品加工费：" prop="categoreCode">
          <el-input v-model="form.productProcessingPrice"></el-input>
        </el-form-item>
        <el-form-item label="配件名称：" prop="categoryName">
          <el-input v-model="form.partsName"></el-input>
        </el-form-item>
        <el-form-item label="配件规格：" prop="weight">
          <el-input v-model="form.partsSpec"></el-input>
        </el-form-item>
        <el-form-item label="配件数量：" prop="length">
          <el-input v-model="form.partsQuantity"></el-input>
        </el-form-item>
        <el-form-item label="配件单价：" prop="width">
          <el-input v-model="form.partsPrice"></el-input>
        </el-form-item>
        <el-form-item label="配件总价：" prop="height">
          <el-input v-model="form.partsTotalPrice"></el-input>
        </el-form-item>
        <el-form-item label="验收标准：" prop="volume">
          <el-input v-model="form.partsAccStand"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
    </el-dialog>
    <div class="btn-warp">
    <el-upload
      class="upload-demo"
      :action="uploadUrl"
      :show-file-list="false"
      :on-success="handleChange"
      :headers="myHeaders"
      >
      <el-button size="small" type="primary">上传</el-button>
    </el-upload>
<!--    &nbsp;&nbsp;&nbsp;-->
<!--    <el-button size="small" type="primary">-->
<!--      <a href="http://120.76.156.209:8000/file/文档/产品导入模板-2024061409222225.xlsx" target="_blank">-->
<!--        下载模板-->
<!--      </a>-->
<!--    </el-button>-->
<!--      <el-button type="primary" @click="deleteCid" >一键删除</el-button >-->
    </div>
    <br/>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      @selection-change="crud.selectionChangeHandler"
      :data="crud.data"
      style="width: 100%"
      border
    >
       <el-table-column :selectable="checkboxT" type="selection" width="55" />
      <el-table-column
        prop="productNames"
        label="产品名称"
      />
      <el-table-column
        prop="productProcessingPrice"
        label="产品加工费"
      />

      <el-table-column
        prop="partsName"
        label="配件名称"
      />
      <el-table-column
        prop="partsSpec"
        label="配件规格"
      />
      <el-table-column
        prop="partsQuantity"
        label="配件数量"
      />
      <el-table-column
        prop="partsPrice"
        label="配件单价"
      />
      <el-table-column
        prop="partsTotalPrice"
        label="配件总价"
      />
      <el-table-column
        prop="partsAccStand"
        label="验收标准"
      />

      <el-table-column
        label="操作"
        width="170px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="margin-right: 3px"
            type="text"
            @click="toEdit(scope.row)"
            >编辑</el-button
          >
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="200"
          >
            <p>确定吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button
                size="mini"
                type="text"
                @click="$refs[scope.row.id].doClose()"
                >取消</el-button
              >
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="delMethod(scope.row.id)"
                >确定</el-button
              >
            </div>
            <el-button slot="reference" type="text" size="mini">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import pagination from "@crud/Pagination";
import DateRangePicker from "@/components/DateRangePicker";
import crudOperation from "@crud/CRUD.operation";
import crudJob from "@/api/wms/productParts/index";
import { getToken } from '@/utils/auth';
import {productNameList} from "../../../../api/wms/productParts";

const defaultForm = {
  productName: "",
  productProcessingPrice: "",
  partsName: "",
  isPartsPublic:"N",
  partsSpec: "",
  publicProductList:"",
  partsQuantity: "",
  partsPrice: "",
  partsTotalPrice: "",
  partsAccStand: "",
};
export default {
  name: "Timing",
  components: { pagination, rrOperation, DateRangePicker, crudOperation },
  cruds() {
    return CRUD({
      title: "",
      url: "productParts/list",
      crudMethod: { ...crudJob },
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: true,
        reset: true,
      },
    });
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      delLoading: false,
      fauserList: [],
      uploadUrl: process.env.VUE_APP_BASE_API + "/productParts/uploadExcel",
      myHeaders: {
        Authorization: getToken()
      },
      permission: {
        // add: ["admin", "company:add"],
        // edit: ["admin", "company:edit"],
        // del: ["admin", "company:del"],
      },
      rules: {
        // jobName: [
        //   { required: true, message: "请输入任务名称", trigger: "blur" },
        // ],
      },
      productNameList:[],

    };
  },
  mounted() {
    this.getProductNameList();
  },
  methods: {
    toEdit(row) {
      this.isEdit = true;
      this.crud.toEdit(row)
      this.form.id = row.id;
    },
    checkboxT(row, rowIndex) {
      return row.id
    },
    isPartsPublicChange(){
      console.log("------",this.form.publicProductList)
      console.log("------",this.form.productName)
      this.form.publicProductList = []
      this.form.productName = ''

    },
    // 选择改变
    selectionChangeHandler(val) {
      crud.selections = val
    },
    getProductNameList(){
      productNameList({  }).then((res) => {


        // 把对象转成数组，key 作为 value，value 作为 label
        this.productNameList = Object.entries(res.data).map(([key, value]) => ({
          value: key,
          label: value
        }))
        // this.parsedData.goodsName = result.items[0].categoryName;
      })
    },

    deleteCid(){
      crudJob
        .deleteCid()
        .then(() => {
          this.delLoading = false;
          this.crud.dleChangePage(1);
          this.crud.delSuccessNotify();
          this.crud.toQuery();
        })
        .catch(() => {
          this.delLoading = false;
        });
    },
    handleChange(file, fileList) {
      console.error(file, fileList);
      this.$notify({
        title: '上传成功！',
        type: 'success',
        duration: 1500
      });
      this.crud.toQuery();
    },
    // 改变状态
    delMethod(id) {
      this.delLoading = true;
      crudJob
        .del([id])
        .then(() => {
          this.delLoading = false;
          this.$refs[id].doClose();
          this.crud.dleChangePage(1);
          this.crud.delSuccessNotify();
          this.crud.toQuery();
        })
        .catch(() => {
          this.delLoading = false;
          this.$refs[id].doClose();
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.btn-warp {
  padding-bottom: 10px;
  display: inline-flex;
}
</style>
